package com.bdzl.flow.controller;

import com.bdzl.flow.service.ActNodeService;
import com.bdzl.flow.service.ActOdrStatusService;
import com.bdzl.pub.controller.BaseController;
import com.bdzl.pub.entity.flow.ActOdrStatusDO;
import com.bdzl.pub.entity.sys.UserDO;
import com.bdzl.pub.util.param.PageUtils;
import com.bdzl.pub.util.param.Query;
import com.bdzl.pub.util.param.R;
import com.bdzl.pub.vo.biz.CheckVO;
import com.bdzl.pub.vo.flow.ActNodeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工作流节点信息表
 *
 * @author yangyicai
 * @email yangyicai@foxmail.com
 * @date 2021-05-12 10:14:58
 */

@Controller
@RequestMapping("/bdzl/actNode")
public class ActNodeController extends BaseController {
	@Autowired
	private ActNodeService actNodeService;
	@Autowired
	private ActOdrStatusService actOdrStatusService;
	/**
	* @Description: TODO 列表查询
	* @param params
	* @return: com.bdzl.pub.util.param.PageUtils
	* @Author: Ming
	* @Date: 2021/5/13 16:17
	*/
	@ResponseBody
	@PostMapping("/list")
	public PageUtils list(@RequestBody Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<ActNodeVO> actNodeList = actNodeService.list(params);
		int total = actNodeService.count(query);
		return new PageUtils(actNodeList, total);
	}


	@GetMapping("/edit/{nodeId}")
	@ResponseBody
	public R edit(@PathVariable("nodeId") Integer nodeId){
		ActNodeVO actNode = actNodeService.getProcess(nodeId);
	    return R.ok(actNode);
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@Transactional
	public R save(@Valid @RequestBody ActNodeVO actNode, HttpServletRequest request){
		//获取当前登录用户数据
		UserDO userInfo = actNodeService.getUserInfo(request);
		actNode.setNodeCreuser(userInfo.getUserId());
		actNode.setNodeCretime(new Date());
		if(actNodeService.save(actNode)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update(@Valid @RequestBody ActNodeVO actNode,HttpServletRequest request){
		//获取当前登录用户数据
		UserDO userInfo = actNodeService.getUserInfo(request);
		actNode.setNodeModuser(userInfo.getUserId());
		actNode.setNodeModtime(new Date());
		actNodeService.update(actNode);
		return R.ok();
	}

	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove(@RequestBody Map<String, Object> params){
		Integer nodeId = (Integer) params.get("nodeId");
		if(actNodeService.remove(nodeId)>0){
		return R.ok();
		}
		return R.error();
	}

	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Integer[] nodeIds){
		actNodeService.batchRemove(nodeIds);
		return R.ok();
	}

	/**
	* @Description: TODO 获取所有节点数据
	* @return: com.bdzl.pub.util.param.R
	* @Author: Ming
	* @Date: 2021/5/14 10:40
	*/
	@GetMapping("/getActNode")
	@ResponseBody
	public R getActNode(){
		List<ActNodeVO> actNodeList = actNodeService.list(new HashMap<>());
		return R.ok(actNodeList);
	}

	/**
	 * @Description: TODO 获取所有状态数据
	 * @return: com.bdzl.pub.util.param.R
	 * @Author: Ming
	 * @Date: 2021/5/14 10:40
	 */
	@GetMapping("/getActOdrStatus")
	@ResponseBody
	public R getActOdrStatus(){
		List<ActOdrStatusDO> actOdrStatusList = actOdrStatusService.list(new HashMap<>());
		return R.ok(actOdrStatusList);
	}

	/**
	 * 启用，禁用节点
	 */
	/**
	* @Description: TODO 启用，禁用节点
	* @param params  nodeId ： 主键  nodeState：节点状态 0 正常，1 停用
	* @return: com.bdzl.pub.util.param.R
	* @Author: Ming
	* @Date: 2021/5/14 17:28
	*/
	@ResponseBody
	@PostMapping("/enable")
	public R enable(@RequestBody Map<String, Object> params){
		ActNodeVO actNodeVO = new ActNodeVO();
		actNodeVO.setNodeId((Integer) params.get("nodeId"));
		actNodeVO.setNodeState((Integer) params.get("nodeState"));
		actNodeService.update(actNodeVO);
		return R.ok();
	}
	/**
	* @Description: TODO 查询所有节点数据,正序排序
	* @param nodeActKey 所属工作流模型
	* @return: com.bdzl.pub.util.param.PageUtils
	* @Author: Ming
	* @Date: 2021/5/18 14:29
	*/
	@ResponseBody
	@PostMapping("/getNode")
	public List<ActNodeVO> getNode(@RequestParam("nodeActKey") String nodeActKey){
		return actNodeService.getNode(nodeActKey);
	}

	/**
	 * @Description: TODO 根据当前节点获取下一节点数据
	 * @param annNodeNow 当前节点编号
	 * @param annFormula 进行下一节点条件公式
	 * @return: java.lang.String
	 * @Author: Ming
	 * @Date: 2021/5/18 16:15
	 */
	@PostMapping("/getNextNodeByNow")
	@ResponseBody
	public ActNodeVO getNextNodeByNow(@RequestParam("annNodeNow") Integer annNodeNow,@RequestParam("annFormula") String annFormula){
		return actNodeService.getNextNodeByNow(annNodeNow,annFormula);
	}

	/**
	 * @Description: TODO 根据当前节点获取驳回节点数据
	 * @param annNodeNow 当前节点编号
	 * @return: java.lang.String
	 * @Author: Ming
	 * @Date: 2021/5/18 16:15
	 */
	/*@PostMapping("/getRejectNodeByNow")
	public ActNodeVO getRejectNodeByNow(@RequestParam("annNodeNow") Integer annNodeNow){
		return actNodeService.getRejectNodeByNow(annNodeNow);
	}*/

	/**
	* @Description: TODO 根据节点id查询数据
	* @param nodeId 节点id
	* @return: com.bdzl.pub.util.param.R
	* @Author: Ming
	* @Date: 2021/5/18 17:38
	*/
	@PostMapping("/getByNodeId")
	@ResponseBody
	public ActNodeVO getByNodeId(@RequestParam("nodeId") Integer nodeId){
		return actNodeService.getProcess(nodeId);
	}


	/**
	 * @Description: TODO 根据订单号查询审核通过，驳回数据
	 * @param acrOdr 订单号
	 * @return: com.bdzl.pub.util.param.R
	 * @Author: Ming
	 * @Date: 2021/5/18 17:38
	 */
	@PostMapping("/getNodeByacrOdr")
	@ResponseBody
	public CheckVO getNodeByacrOdr(@RequestParam("acrOdr") String acrOdr){
		return actNodeService.getNodeByacrOdr(acrOdr);
	}

	/**
	 * @Description: TODO 根据订单状态的值查询下一步节点数据
	 * @param statVal 订单状态
	 * @return: com.bdzl.pub.util.param.R
	 * @Author: Ming
	 * @Date: 2021/5/18 17:38
	 */
	@PostMapping("/getOdrStatusByStatVal")
	@ResponseBody
	public List<ActNodeVO> getOdrStatusByStatVal(@RequestParam("statVal") String statVal){
		return actNodeService.getOdrStatusByStatVal(statVal);
	}
}
